package chart;

import java.awt.Color;
import java.awt.Paint;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.imageio.ImageIO;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

import org.jCharts.axisChart.AxisChart;
import org.jCharts.chartData.AxisChartDataSet;
import org.jCharts.chartData.ChartDataException;
import org.jCharts.chartData.DataSeries;
import org.jCharts.properties.AxisProperties;
import org.jCharts.properties.BarChartProperties;
import org.jCharts.properties.ChartProperties;
import org.jCharts.properties.LegendProperties;
import org.jCharts.properties.PropertyException;
import org.jCharts.types.ChartType;
import org.xml.sax.SAXException;

import entity.Income;
import exception.NullVariableFound;

import settings.MasterController;

//@author U096819H
public class BarChartIncome {

	private String path = "graphs";
	
	//@author U096819H
	public void generateChart(MasterController mc, Calendar _fromDate, Calendar _toDate) throws ChartDataException,
			PropertyException, TransformerConfigurationException,
			InstantiationException, IllegalAccessException, JAXBException,
			ParserConfigurationException, SAXException, IOException,
			TransformerException, NullVariableFound {

		ArrayList<Income> listIncome = mc.GetIncomeListByDateRange(_fromDate, _toDate);
		String[] xAxisLabels = new String[listIncome.size()];
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		for (int i = 0; i < listIncome.size(); i++) {
			xAxisLabels[i] = dateFormat.format(listIncome.get(i).getDate().getTime());
		}
		String xAxisTitle = " ";
		String yAxisTitle = "Amount";
		String title = "Income";
		DataSeries dataSeries = new DataSeries(xAxisLabels, xAxisTitle,
				yAxisTitle, title);

		double[][] data = new double[1][listIncome.size()];
		String id;
		for (int i = 0; i < listIncome.size(); i++) {
			data[0][i] = listIncome.get(i).getAmount();
		}

		String[] legendLabels = new String[1];
		legendLabels[0] = "Income";
		Paint[] paints = new Paint[] { Color.blue };
		BarChartProperties barChartProperties = new BarChartProperties();
		AxisChartDataSet axisChartDataSet = new AxisChartDataSet(data,
				legendLabels, paints, ChartType.BAR, barChartProperties);
		dataSeries.addIAxisPlotDataSet(axisChartDataSet);

		ChartProperties chartProperties = new ChartProperties();

		AxisProperties axisProperties = new AxisProperties();
		LegendProperties legendProperties = new LegendProperties();
		AxisChart axisChart = new AxisChart(dataSeries, chartProperties,
				axisProperties, legendProperties, 774, 411);

		BufferedImage bufferedImage = new BufferedImage(774, 411,
				BufferedImage.TYPE_INT_RGB);
		axisChart.setGraphics2D(bufferedImage.createGraphics());
		axisChart.render();
		try {
			// retrieve image
			File outputfile = new File("graphs", "barChart.png");
			ImageIO.write(bufferedImage, "png", outputfile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
